package com.apestech.framework.db.session;

import com.apestech.framework.db.SqlSession;
import com.apestech.framework.db.callback.TableJsonCallBackHandler;
import com.apestech.framework.db.queryData.QueryJson;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
/**
 * @author hup
 * @create 2018-08-31-14:44
 */
public class ForJSON {

    private SqlSession session;

    public ForJSON(SqlSession session){
        this.session=session;
    }

	public JSONArray query(String sql) throws Exception{
        QueryJson query=new QueryJson();
        session.getForResultSet().sql(sql, query);
        return query.getList();
	}

    public JSONArray query(String sql, JSONObject param) throws Exception{
        QueryJson query=new QueryJson();
        session.getForResultSet().sql(sql, param, query);
        return query.getList();
    }

    public JSONArray query(String sql, JSONObject param, TableJsonCallBackHandler tableJsonCallBackHandler) throws Exception{
        QueryJson query=new QueryJson();
        query.setTableJsonCallBackHandler(param.getString("tablename"), tableJsonCallBackHandler);
        session.getForResultSet().sql(sql, param, query);
        return query.getList();
    }

    public JSONArray queryId(String queryid, JSONObject param) throws Exception {
        QueryJson query=new QueryJson();
        try {
            session.getForResultSet().queryId(queryid, param, query);
            return query.getList();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public JSONArray table(String tabName, JSONObject param) throws Exception {
        QueryJson query=new QueryJson();
        try {
            return execTable(tabName, param, query);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public JSONArray table(String tabName, JSONObject param, TableJsonCallBackHandler tableJsonCallBackHandler) throws Exception {
        QueryJson query=new QueryJson();
        try {
            query.setTableJsonCallBackHandler(tabName, tableJsonCallBackHandler);
            return execTable(tabName, param, query);
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    private JSONArray execTable(String tabName, JSONObject param, QueryJson query) throws Exception {
        try {
            session.getForResultSet().table(tabName, param, query);
            return query.getList();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public JSONArray table(String tabName) throws Exception {
        return table(tabName, null);
    }

    public JSONArray tableId(String tableId, JSONObject param) throws Exception {
        QueryJson query=new QueryJson();
        try {
            session.getForResultSet().tableId(tableId, param, query);
            return query.getList();
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public JSONArray tableId(String tableId) throws Exception {
        return tableId(tableId, null);
    }

    public synchronized String getTabComment(String tabName) throws Exception {
        JSONObject m=new JSONObject();
        m.put("autoCompletion","0");
        m.put("TABLE_NAME", tabName);
        m.put("printSql","false");
        String sql="SELECT COMMENTS FROM USER_TAB_COMMENTS WHERE TABLE_NAME=#{TABLE_NAME}";
        JSONArray qlist=query(sql, m);
        if(qlist.size()==0) return tabName;
        return qlist.get(0).getString("COMMENTS");
    }

}
